﻿using System.Data.Entity.Migrations.Model;
using System.Linq;
using System.Net.Sockets;
using System.Runtime.InteropServices;
using System.Runtime.Remoting.Messaging;
using NewBusiness;
using NewBusiness.Domain;
using NewBusiness.Domain.Queries;
using NewBusiness.Domain.Uow;


namespace TMS.NewBusiness.Domain
{
    public interface IWorkItemsContext : IDomainContext
    {
        IWorkItemsQueries WorkItemsQueries { get; }
        IQueryable<WorkItem> GetWorkItemsForTeam(int id, int page, int itemsPerPage = 10);
        void SetNewPriorityForWorkItem(int id, int beforeId, int afterId);
        IQueryable<dynamic> GetWorkItemsPerUsers();
    }
    public class WorkItemsContext : DomainContext, IWorkItemsContext
    {
        public IWorkItemsQueries WorkItemsQueries { get; private set; }
        public WorkItemsContext(IUow uow, ISession session, IWorkItemsQueries workItemsQueries, MongoDB.Driver.MongoDatabase mdb)
            : base(uow, session, mdb)
        {
            this.WorkItemsQueries = workItemsQueries;
        }


        public IQueryable<WorkItem> GetWorkItemsForTeam(int id, int page, int itemsPerPage = 10)
        {
            return this.WorkItemsQueries.GetWorkItemsForTeam(id, page, itemsPerPage);
        }

        public void SetNewPriorityForWorkItem(int id, int beforeId, int afterId)
        {
            this.WorkItemsQueries.SetNewPriorityForWorkItem(id,beforeId,afterId);
        }

        public IQueryable<dynamic> GetWorkItemsPerUsers()
        {
        //    var wiq = this.Uow.WorkItemQueue.GetAll();
        //    var wis = this.Uow.WorkItems.GetAll().Where(wi => new[] {6, 7, 10, 15, 19}.Contains(wi.CurrentStatusId)); //криво переписать.
        //    var r = from wi in wis
        //        join wq in wiq on wi.Id equals wq.WorkItemId
        //        group wi by wq.UserId;
        //    return from g in r
        //        select new {UserId = g.Key, Tasks = g};
            return null;
        }
    }
}
